PROJECT(MJIN2)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
CMAKE_POLICY(VERSION 2.6)
SET(BUILD_FAST       YES        CACHE BOOL "Build everything in bulk")
SET(BUILD_MJIN2      YES        CACHE BOOL "Build MJIN2")
SET(BUILD_PYTHON     YES        CACHE BOOL "Build Python MJIN2 wrapper")
SET(BUILD_TESTS      NO         CACHE BOOL "Build tests")
SET(BUILD_WITH_GPROF NO         CACHE BOOL "Build with profiling support for gprof")
SET(MINGW_DIR        "C:/MinGW" CACHE PATH "Path to MinGW (dependencies)")
SET(DARWIN_DIR       "~/Darwin" CACHE PATH "Path to Darwin (dependencies)")
SET(USE_PUGIXML      NO         CACHE BOOL "Use pugixml as XML parser. OSG otherwise.")

# Common.
SET(ALURE_LIB      alure)
SET(PYTHON_LIB_EXT so)
SET(PYTHON_MJIN2_DIR ${MJIN2_BINARY_DIR})
# Platform specific.
# Windows.
IF (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    SET(STD_INC_DIR ${MINGW_DIR}/include ${MINGW_DIR}/include/AL)
    ADD_DEFINITIONS(-DWIN32 -U__STRICT_ANSI__)
    SET(ALURE_LIB      ALURE32)
    SET(OPENAL_LIB     OpenAL32)
    SET(OPENGL_LIB     OpenGL32)
    SET(PYTHON_LIB_EXT pyd)
# Mac OS X.
ELSEIF (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
    SET(STD_INC_DIR /usr/include ${DARWIN_DIR}/include ${DARWIN_DIR}/include/AL)
    SET(STD_LIB_DIR ${DARWIN_DIR}/lib)
    FIND_LIBRARY(OPENAL NAMES OpenAL)
    SET(OPENAL_LIB ${OPENAL})
    FIND_LIBRARY(OPENGL NAMES OpenGL)
    SET(OPENGL_LIB ${OPENGL})
    SET(PYTHON_LIB_DIR_PREFIX ${CMAKE_INSTALL_PREFIX})
    SET(PYTHON_MJIN2_DIR ${MJIN2_BINARY_DIR}/Release)
# Linux.
ELSE ()
    SET(STD_INC_DIR /usr/include
                    /usr/include/AL
                    /usr/local/include
                    /usr/local/include/AL)
    SET(OPENAL_LIB     openal)
    SET(OPENGL_LIB     GL)
ENDIF ()
# Additional modules.
SET(CMAKE_MODULE_PATH ${MJIN2_SOURCE_DIR})
INCLUDE(FastBuild)
IF (BUILD_WITH_GPROF)
    ADD_DEFINITIONS(-pg)
ENDIF ()
IF (USE_PUGIXML)
    ADD_DEFINITIONS(-DMJIN2_USE_PUGIXML)
ENDIF ()
INCLUDE_DIRECTORIES(${MJIN2_SOURCE_DIR}/mjin2/include
                    ${MJIN2_SOURCE_DIR}/mjin2priv/include
                    ${MJIN2_SOURCE_DIR}
                    ${STD_INC_DIR})
LINK_DIRECTORIES(${STD_LIB_DIR})
# Read VERSION.
FILE(READ VERSION MJIN2_VERSION)
# Enable all compiler warnings, debug symbols. Define VERSION.
ADD_DEFINITIONS(-Wall -g -std=c++0x -DMJIN2_VERSION="${MJIN2_VERSION}")
# Doxygen.
FIND_PACKAGE(Doxygen)
IF (DOXYGEN_FOUND)
    CONFIGURE_FILE(${MJIN2_SOURCE_DIR}/Doxyfile.in ${MJIN2_BINARY_DIR}/Doxyfile)
    ADD_CUSTOM_TARGET(doc
                      ${DOXYGEN_EXECUTABLE}
                      ${MJIN2_BINARY_DIR}/Doxyfile
                      WORKING_DIRECTORY ${MJIN2_BINARY_DIR}
                      COMMENT "Generate Doxygen documentation")
ENDIF ()
# MJIN2.
IF (BUILD_MJIN2)
    SET(LIBNAME mjin2-${MJIN2_VERSION})
    # Dependency library.
    ADD_LIBRARY(mt19937ar STATIC mt19937ar/mt19937ar.cpp)
    SET_PROPERTY(TARGET mt19937ar PROPERTY COMPILE_FLAGS -fPIC)
    # Private.
    SET(PRIVATE 
                mjin2priv/src/mjin2/priv/Application.cpp
                mjin2priv/src/mjin2/priv/Camera.cpp
                mjin2priv/src/mjin2/priv/Common.cpp
                mjin2priv/src/mjin2/priv/ConfigFile.cpp
                mjin2priv/src/mjin2/priv/Input.cpp
                mjin2priv/src/mjin2/priv/InputTranslator.cpp
                mjin2priv/src/mjin2/priv/Light.cpp
                mjin2priv/src/mjin2/priv/Logger.cpp
                mjin2priv/src/mjin2/priv/LoggerOSG.cpp
                mjin2priv/src/mjin2/priv/PathResolver.cpp
                mjin2priv/src/mjin2/priv/RegExp.cpp
                mjin2priv/src/mjin2/priv/Scene.cpp
                mjin2priv/src/mjin2/priv/SceneCamera.cpp
                mjin2priv/src/mjin2/priv/SceneGroup.cpp
                mjin2priv/src/mjin2/priv/SceneLight.cpp
                mjin2priv/src/mjin2/priv/SceneLine.cpp
                mjin2priv/src/mjin2/priv/SceneMesh.cpp
                mjin2priv/src/mjin2/priv/SceneNode.cpp
                mjin2priv/src/mjin2/priv/SceneNodeFactory.cpp
                mjin2priv/src/mjin2/priv/SceneNodeSelector.cpp
                mjin2priv/src/mjin2/priv/Sequence.cpp
                mjin2priv/src/mjin2/priv/Timer.cpp
                mjin2priv/src/mjin2/priv/Viewer.cpp
                mjin2priv/src/mjin2/priv/Window.cpp
                mjin2priv/src/mjin2/priv/WindowClose.cpp
                mjin2priv/src/mjin2/priv/WindowGeometry.cpp
                mjin2priv/src/mjin2/priv/WindowRecreation.cpp
                mjin2priv/src/mjin2/priv/XML.cpp
                mjin2priv/src/mjin2/priv/XMLOSG.cpp
                mjin2priv/src/mjin2/priv/XMLPugi.cpp)
    IF (BUILD_FAST)
        FAST_BUILD_FILES(mjin2priv.cpp
                         ${MJIN2_SOURCE_DIR}
                         ${MJIN2_BINARY_DIR}
                         ${PRIVATE})
        ADD_LIBRARY(mjin2priv STATIC ${MJIN2_BINARY_DIR}/mjin2priv.cpp)
    ELSE ()
        ADD_LIBRARY(mjin2priv STATIC ${PRIVATE})
    ENDIF ()
    SET_PROPERTY(TARGET mjin2priv PROPERTY COMPILE_FLAGS -fPIC)
    # Public.
    SET(PUBLIC mjin2/src/mjin2/Application.cpp
               mjin2/src/mjin2/Camera.cpp
               mjin2/src/mjin2/Common.cpp
               mjin2/src/mjin2/ConfigFile.cpp
               mjin2/src/mjin2/Exception.cpp
               mjin2/src/mjin2/FileStream.cpp
               mjin2/src/mjin2/InputTranslator.cpp
               mjin2/src/mjin2/Light.cpp
               mjin2/src/mjin2/Logger.cpp
               mjin2/src/mjin2/PathResolver.cpp
               mjin2/src/mjin2/RegExp.cpp
               mjin2/src/mjin2/Scene.cpp
               mjin2/src/mjin2/SceneCamera.cpp
               mjin2/src/mjin2/SceneGroup.cpp
               mjin2/src/mjin2/SceneLight.cpp
               mjin2/src/mjin2/SceneLine.cpp
               mjin2/src/mjin2/SceneMesh.cpp
               mjin2/src/mjin2/SceneNode.cpp
               mjin2/src/mjin2/SceneNodeFactory.cpp
               mjin2/src/mjin2/SceneNodeSelector.cpp
               mjin2/src/mjin2/Sequence.cpp
               mjin2/src/mjin2/SoundBuffer.cpp
               mjin2/src/mjin2/SoundExceptions_p.cpp
               mjin2/src/mjin2/SoundSource.cpp
               mjin2/src/mjin2/SoundSourceBackground.cpp
               mjin2/src/mjin2/SoundSourceBuffer.cpp
               mjin2/src/mjin2/SoundSourceMultiBuffer.cpp
               mjin2/src/mjin2/SoundSourceStream.cpp
               mjin2/src/mjin2/SoundStreamIO_p.cpp
               mjin2/src/mjin2/SoundSystem.cpp
               mjin2/src/mjin2/Timer.cpp
               mjin2/src/mjin2/Vec3.cpp
               mjin2/src/mjin2/Vec4.cpp
               mjin2/src/mjin2/VideoInfo.cpp
               mjin2/src/mjin2/Window.cpp
               mjin2/src/mjin2/XML.cpp)
    IF (BUILD_FAST)
        FAST_BUILD_FILES(mjin2public.cpp
                         ${MJIN2_SOURCE_DIR}
                         ${MJIN2_BINARY_DIR}
                         ${PUBLIC})
        ADD_LIBRARY(${LIBNAME} SHARED ${MJIN2_BINARY_DIR}/mjin2public.cpp)
    ELSE ()
        ADD_LIBRARY(${LIBNAME} SHARED ${PUBLIC})
    ENDIF ()
    SET(PUGIXML_LIB "")
    IF (USE_PUGIXML)
        SET(PUGIXML_LIB pugixml)
    ENDIF ()
    # Final MJIN2 library.
    TARGET_LINK_LIBRARIES(${LIBNAME}
                          ${ALURE_LIB}
                          mjin2priv
                          mt19937ar
                          ${OPENAL_LIB}
                          ${OPENGL_LIB}
                          OpenThreads
                          osg
                          osgDB
                          osgGA
                          osgShadow
                          osgViewer
                          osgUtil
                          pcre
                          ${PUGIXML_LIB})
    IF (BUILD_WITH_GPROF)
        SET_PROPERTY(TARGET ${LIBNAME} PROPERTY LINK_FLAGS -pg)
    ENDIF ()
    # Install library.
    INSTALL(TARGETS ${LIBNAME}
            RUNTIME DESTINATION bin
            LIBRARY DESTINATION lib
            ARCHIVE DESTINATION lib)
    # Install headers.
    INSTALL(FILES mjin2/include/mjin2/Application.h
                  mjin2/include/mjin2/Camera.h
                  mjin2/include/mjin2/Common.h
                  mjin2/include/mjin2/Exception.h
                  mjin2/include/mjin2/FileStream.h
                  mjin2/include/mjin2/InputListener.h
                  mjin2/include/mjin2/InputTranslator.h
                  mjin2/include/mjin2/Light.h
                  mjin2/include/mjin2/ListenerMacros.h
                  mjin2/include/mjin2/Logger.h
                  mjin2/include/mjin2/mjin2.h
                  mjin2/include/mjin2/PathResolver.h
                  mjin2/include/mjin2/RegExp.h
                  mjin2/include/mjin2/Scene.h
                  mjin2/include/mjin2/SceneCamera.h
                  mjin2/include/mjin2/SceneGroup.h
                  mjin2/include/mjin2/SceneLight.h
                  mjin2/include/mjin2/SceneLine.h
                  mjin2/include/mjin2/SceneMesh.h
                  mjin2/include/mjin2/SceneNode.h
                  mjin2/include/mjin2/SceneNodeFactory.h
                  mjin2/include/mjin2/SceneNodeSelector.h
				  mjin2/include/mjin2/SoundBuffer.h
                  mjin2/include/mjin2/SoundSource.h
                  mjin2/include/mjin2/SoundSourceBackground.h
                  mjin2/include/mjin2/SoundSourceMultiBuffer.h
                  mjin2/include/mjin2/SoundSourceStream.h
                  mjin2/include/mjin2/SoundSystem.h
                  mjin2/include/mjin2/SoundTypes.h
                  mjin2/include/mjin2/TypeDefs.h
                  mjin2/include/mjin2/Vec3.h
                  mjin2/include/mjin2/Vec4.h
                  mjin2/include/mjin2/Window.h
                  mjin2/include/mjin2/WindowConfig.h
                  mjin2/include/mjin2/WindowListener.h
                  mjin2/include/mjin2/XML.h
            DESTINATION include/mjin2)
ENDIF ()
# Python.
IF (BUILD_PYTHON)
    FIND_PACKAGE(SWIG REQUIRED)
    INCLUDE(${SWIG_USE_FILE})
    SET(Python_ADDITIONAL_VERSIONS "2.7")
    FIND_PACKAGE(PythonInterp)
    # Module install directory.
    EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -Oc "import distutils.sysconfig; print distutils.sysconfig.get_python_lib()"
                    OUTPUT_VARIABLE PYTHON_LIB_DIR)
    FIND_PACKAGE(PythonLibs)
    INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH} ${MJIN2_SOURCE_DIR}/mjin2/include)
    SET(CMAKE_SWIG_FLAGS "")
    SET_SOURCE_FILES_PROPERTIES(mjin2.i PROPERTIES CPLUSPLUS ON)
    SET_SOURCE_FILES_PROPERTIES(mjin2.i PROPERTIES SWIG_FLAGS "-includeall")
    SWIG_ADD_MODULE(mjin2 python mjin2.i)
    SWIG_LINK_LIBRARIES(mjin2 ${PYTHON_LIBRARIES} ${LIBNAME})
    # Strip newline.
    STRING(STRIP ${PYTHON_LIB_DIR} PYTHON_LIB_DIR)
    #ADD_CUSTOM_TARGET(mjin2.pyc ${PYTHON_EXECUTABLE} -m compileall mjin2.py)
    # Install library.
    INSTALL(FILES       ${PYTHON_MJIN2_DIR}/_mjin2.${PYTHON_LIB_EXT}
                        ${MJIN2_BINARY_DIR}/mjin2.py
            DESTINATION ${PYTHON_LIB_DIR_PREFIX}${PYTHON_LIB_DIR})
    INSTALL(FILES pymjin2/__init__.py
                  pymjin2/Common.py
                  pymjin2/GameApplication.py
                  pymjin2/GameConfig.py
                  pymjin2/GameObjectListener.py
                  pymjin2/GameObject.py
                  pymjin2/GamePlayer.py
                  pymjin2/GameScene.py
                  pymjin2/GameWorld.py
                  pymjin2/GameXML.py
                  pymjin2/SceneSerializer.py
                  pymjin2/Translator.py
            DESTINATION ${PYTHON_LIB_DIR_PREFIX}${PYTHON_LIB_DIR}/pymjin2)
ENDIF ()
# Tests.
IF (BUILD_TESTS)
    LINK_DIRECTORIES(${MJIN2_BINARY_DIR})
    # Renderless.
    FAST_BUILD_FILES(testRenderless.cpp
                     ${MJIN2_SOURCE_DIR}
                     ${MJIN2_BINARY_DIR}
                     t/Renderless/main.cpp)
    ADD_EXECUTABLE(testRenderless ${MJIN2_BINARY_DIR}/testRenderless.cpp)
    TARGET_LINK_LIBRARIES(testRenderless ${LIBNAME})
    # MJIN2.
    FAST_BUILD_FILES(testMJIN2.cpp
                     ${MJIN2_SOURCE_DIR}
                     ${MJIN2_BINARY_DIR}
                     t/MJIN2/main.cpp)
    ADD_EXECUTABLE(testMJIN2 ${MJIN2_BINARY_DIR}/testMJIN2.cpp)
    TARGET_LINK_LIBRARIES(testMJIN2 ${LIBNAME})
ENDIF ()
